import api from '@/api';
import { defineStore } from 'pinia';
import { useUserStore } from './user';
import { useMenuStore } from './menu';

export const useAppStore = defineStore('app', {
  state: () => ({
    token: '',
  }),
  actions: {
    async login(params: LoginParams) {
      const token = await api.login(params);
      this.token = token;
      // 登陆成功之后 此信息是必须获取的
      await Promise.all([useUserStore().syncUser(), useMenuStore().syncMenu()]);
    },
    logout() {
      localStorage.clear();
      sessionStorage.clear();
      location.href = '/login';
    },
  },
  persist: true,
});
